from utilities import *

from toee import *

from Co8 import *

def san_dialog( attachee, triggerer ):
	if (find_npc_near( attachee, 8032 ) != OBJ_HANDLE_NULL):
		return RUN_DEFAULT
	if (game.global_flags[144] == 1):
		attachee.attack(triggerer)
	elif (not attachee.has_met(triggerer)):
		triggerer.begin_dialog( attachee, 1 )
	else:
		triggerer.begin_dialog( attachee, 110 )
	return SKIP_DEFAULT

def san_will_kos( attachee, triggerer ):
	if (triggerer.type == obj_t_pc):
		if (game.global_flags[144] == 1):
			return RUN_DEFAULT
	return SKIP_DEFAULT

def san_first_heartbeat( attachee, triggerer ):
	if (attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
		attachee.cast_spell(spell_stoneskin, attachee)
		attachee.spells_pending_to_memorized()
		game.global_vars[720] = 1
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	while(attachee.item_find(8008) != OBJ_HANDLE_NULL):
		attachee.item_find(8008).destroy()
	while(attachee.item_find(8009) != OBJ_HANDLE_NULL):
		attachee.item_find(8009).destroy()
	while(attachee.item_find(8010) != OBJ_HANDLE_NULL):
		attachee.item_find(8010).destroy()
	while(attachee.item_find(8013) != OBJ_HANDLE_NULL):
		attachee.item_find(8013).destroy()
	while(attachee.item_find(8014) != OBJ_HANDLE_NULL):
		attachee.item_find(8014).destroy()
	while(attachee.item_find(8015) != OBJ_HANDLE_NULL):
		attachee.item_find(8015).destroy()
	while(attachee.item_find(8017) != OBJ_HANDLE_NULL):
		attachee.item_find(8017).destroy()
	while(attachee.item_find(8020) != OBJ_HANDLE_NULL):
		attachee.item_find(8020).destroy()
	while(attachee.item_find(8021) != OBJ_HANDLE_NULL):
		attachee.item_find(8021).destroy()
	while(attachee.item_find(8022) != OBJ_HANDLE_NULL):
		attachee.item_find(8022).destroy()
	while(attachee.item_find(8024) != OBJ_HANDLE_NULL):
		attachee.item_find(8024).destroy()
	while(attachee.item_find(8025) != OBJ_HANDLE_NULL):
		attachee.item_find(8025).destroy()
	while(attachee.item_find(8027) != OBJ_HANDLE_NULL):
		attachee.item_find(8027).destroy()
	while(attachee.item_find(8030) != OBJ_HANDLE_NULL):
		attachee.item_find(8030).destroy()
		create_item_in_inventory( 8904, attachee )
	if (not game.combat_is_active()):
		if (game.global_flags[144] == 0):
			if (find_npc_near( attachee, 8032 ) == OBJ_HANDLE_NULL):
				for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
					if (not attachee.has_met(obj)):
						if (is_safe_to_talk(attachee,obj)):
							obj.begin_dialog(attachee,1)
#							game.new_sid = 0			## removed by Livonya
	if (game.combat_is_active()):
		return RUN_DEFAULT
	if (game.global_vars[720] == 0):
			attachee.cast_spell(spell_stoneskin, attachee)
			attachee.spells_pending_to_memorized()
	if (game.global_vars[720] == 4):
			attachee.cast_spell(spell_see_invisibility, attachee)
			attachee.spells_pending_to_memorized()
	if (game.global_vars[720] == 8):
			attachee.cast_spell(spell_false_life, attachee)
			attachee.spells_pending_to_memorized()
	if (game.global_vars[720] == 12):
			attachee.cast_spell(spell_mage_armor, attachee)
			attachee.spells_pending_to_memorized()
	game.global_vars[720] = game.global_vars[720] + 1	
	if (game.global_vars[720] >= 200):
		game.global_vars[720] = 1

	return RUN_DEFAULT

def san_dying( attachee, triggerer ):
	if game.global_flags[992] == 0:
		game.global_flags[147] = 1
		create_item_in_inventory( 8008, attachee )
		create_item_in_inventory( 8009, attachee )
		create_item_in_inventory( 8010, attachee )
		create_item_in_inventory( 8013, attachee )
		create_item_in_inventory( 8014, attachee )
		create_item_in_inventory( 8015, attachee )
		create_item_in_inventory( 8017, attachee )
		create_item_in_inventory( 8020, attachee )
		create_item_in_inventory( 8021, attachee )
		create_item_in_inventory( 8022, attachee )
		create_item_in_inventory( 8024, attachee )
		create_item_in_inventory( 8025, attachee )
		create_item_in_inventory( 8027, attachee )
		create_item_in_inventory( 8030, attachee )
		return RUN_DEFAULT
	else:
		while(attachee.item_find(6219) != OBJ_HANDLE_NULL):
			attachee.item_find(6219).destroy()
		game.global_flags[992] = 0
		return SKIP_DEFAULT

def san_resurrect( attachee, triggerer ):
	game.global_flags[147] = 0
	return RUN_DEFAULT

def senshock_kills_hedrack( attachee, triggerer ):
	game.timevent_add( senshock_check_kill, ( attachee, ), 7200000 )
	return RUN_DEFAULT

def senshock_check_kill( attachee ):
	if (game.global_flags[146] == 0):
		if (game.global_flags[147] == 0):
			attachee.object_flag_set(OF_OFF)
			game.global_flags[146] = 1
	return RUN_DEFAULT

def san_start_combat( attachee, triggerer ):
	if ( obj_percent_hp(attachee) < 50):
		found_pc = OBJ_HANDLE_NULL
		StopCombat(attachee, 0)  # Added by Cerulean the Blue:  StopCombat is in Co8.py
		for pc in game.party:
			if pc.type == obj_t_pc:
				found_pc = pc
				attachee.ai_shitlist_remove( pc )
		if found_pc != OBJ_HANDLE_NULL:
			found_pc.turn_towards(attachee)		## added by Livonya
			attachee.turn_towards(found_pc)		## added by Livonya
			found_pc.begin_dialog( attachee, 190 )
			game.new_sid = 0
			return SKIP_DEFAULT
	return RUN_DEFAULT

def senshock_escape( attachee, triggerer ):
	attachee.runoff(attachee.location-3)
	return RUN_DEFAULT

def turn_off(attachee, triggerer):
	game.timevent_add( resume_combat, ( attachee, triggerer ),500)
	attachee.object_flag_unset(OF_OFF)
	attachee.destroy ( attachee )
	return

def resume_combat(attachee, triggerer):
	for obj in game.obj_list_vicinity( attachee.location, OLC_CRITTERS):
		obj.attack(triggerer)
	return
